package com.zr.planClass.service.impl;

import com.zr.planClass.mapper.PlanClassMinMapper;
import com.zr.planClass.model.modelmin.*;
import com.zr.planClass.service.PlanClassMinService;
import com.zr.util.AllRecords;
import com.zr.util.ErrorMessageClass;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by 非常狂躁的猪 on 2019/12/24.
 */
@Service
public class PlanClassMinServiceImpl implements PlanClassMinService {
    @Autowired
    private PlanClassMinMapper planClassMinMapper;

    @Override
    @Transactional   //一定要注意打上事务注解
    public ResultVo add(@Valid PlanClassAddVo planClassAddVo) {


        ResultVo resultVoYanZheng= yanZheng(planClassAddVo);
        if (!resultVoYanZheng.getSuccess()){
            return resultVoYanZheng;
        }


        PlanClassVo planClassVo = new PlanClassVo();
        BeanUtils.copyProperties(planClassAddVo,planClassVo);
        Date nowDate = new Date();
        planClassVo.setCreateName("张三");
        planClassVo.setCreateTime(nowDate);
        planClassVo.setUpdateName("张三");
        planClassVo.setUpdateTime(nowDate);
        planClassMinMapper.addPlanClass(planClassVo);

        return ResultBuilderVo.success();
    }

    public ResultVo yanZheng(PlanClassAddVo planClassAddVo){
        //根据前端传过来的名称查询数据库,查看是否存在
        int nameCount =  planClassMinMapper.queryByName(planClassAddVo.getName());
        if (nameCount >0){
            return ResultBuilderVo.error(ErrorMessageClass.PLANLASSSADD); //ResultVo只是用来返回的,对这个类进行单纯的赋值操作
        }
        //根据前端传过来的编码查询数据库,查看是否存在
        int codeCount =  planClassMinMapper.queryByCode(planClassAddVo.getCode());
        if (codeCount >0){
            return ResultBuilderVo.error("编码已经存在"); //ResultVo只是用来返回的,对这个类进行单纯的赋值操作
        }

        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid PlanClassUpdateVo planClassUpdateVo) {
        PlanClassUpdateVo getPlanByid = planClassMinMapper.queryById(planClassUpdateVo.getId());//判断id是否存在

        if (getPlanByid == null){
            return ResultBuilderVo.error(ErrorMessageClass.CODEISNO);
        }

        PlanClassUpdateVo getPlanByName = planClassMinMapper.getPlanByName(planClassUpdateVo.getName()); //判断修改的名称是否存在,但是,可以为自己
        if (getPlanByName != null ){
            if ( !getPlanByName.getId().equals(planClassUpdateVo.getId()) ){
                return ResultBuilderVo.error(ErrorMessageClass.PLANLASSSADD);
            }
        }
        //接着就进行修改
        PlanClassVo planClassVo = new PlanClassVo();
        //BeanUtils.copyProperties(planClassAddVo,planClassVo);
        BeanUtils.copyProperties(planClassUpdateVo,planClassVo);
        Date date = new Date();
        planClassVo.setUpdateTime(date);
        planClassVo.setUpdateName("张飞");


        planClassMinMapper.update(planClassVo);

        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo queryOnePlan(Integer id) {
        PlanClassUpdateVo planClassUpdateVo1 = planClassMinMapper.queryById(id);
        if (planClassUpdateVo1 != null ){
            return ResultBuilderVo.success(planClassUpdateVo1);
        }


        return ResultBuilderVo.success(planClassUpdateVo1);
    }

    @Override
    public ResultVo queryAllPlan(PlanClassSelectVo planClassSelectVo) {
        //1.查询条数
        //2.查询数据
        System.out.println("aaaaaaaaaaaa"+planClassSelectVo.getOffset());
        List<PlanClassVo> planClassVoList = planClassMinMapper.queryAllPlan(planClassSelectVo);
        if (!CollectionUtils.isEmpty(planClassVoList)){
            for(PlanClassVo planClassVo:planClassVoList){
                if (!planClassVo.getStatus()){
                    planClassVo.setStatusName("禁用");
                }
                if (planClassVo.getStatus()){
                    planClassVo.setStatusName("启用");
                }

            }
        }
        int count = planClassMinMapper.queryCount(planClassSelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(planClassVoList);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(planClassSelectVo.getPageIndex());
        allRecords.setPageSize(planClassSelectVo.getPageSize());
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    public ResultVo updateStatusById(@Valid PlanClassUpdateStatus planClassUpdateStatus) {

        int countId = planClassMinMapper.updateStatusQueryById(planClassUpdateStatus.getId());

        if (countId <= 0){
            return  ResultBuilderVo.error(ErrorMessageClass.IDISNO);
        }

        PlanClassVo planClassVo = new PlanClassVo();
        BeanUtils.copyProperties(planClassUpdateStatus,planClassVo);
        Date date = new Date();
        planClassVo.setUpdateTime(date);
        planClassVo.setUpdateName("张三");
        planClassMinMapper.updateStatusById(planClassVo);
        return ResultBuilderVo.success();
    }




}



































